//
//  SubTagController.m
//  Budejie
//
//  Created by 远成物流 on 2017/6/1.
//  Copyright © 2017年 陈梦佳. All rights reserved.
//

#import "SubTagController.h"
#import <SVProgressHUD/SVProgressHUD.h>
#import <MJRefresh/MJRefresh.h>
#import <AFNetworking/AFNetworking.h>
#import "SubTagModel.h"
#import <MJExtension/MJExtension.h>
#import "SubTagCell.h"

@interface SubTagController ()

@property(nonatomic,weak)AFHTTPSessionManager *manager;
@property(nonatomic,strong)NSMutableArray *items;

@end

static NSString * const cellID = @"SubTagCell";

@implementation SubTagController

-(AFHTTPSessionManager *)manager{
    if (_manager == nil) {
        _manager = [AFHTTPSessionManager manager];
    }
    
    return _manager;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"推荐订阅";
    
    [self.tableView registerNib:[UINib nibWithNibName:@"SubTagCell" bundle:nil] forCellReuseIdentifier:cellID];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self loadData];
    }];
    self.tableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        [self loadMoreData];
    }];
    
    [self.tableView.mj_header beginRefreshing];
}

//下拉刷新
-(void)loadData{

    [self.manager.tasks makeObjectsPerformSelector:@selector(cancel)];
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"a"] = @"tag_recommend";
    parameters[@"action"] = @"sub";
    parameters[@"c"] = @"topic";
    
    [self.manager GET:CommonURL parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        [self.tableView.mj_header endRefreshing];
        
        _items = [SubTagModel mj_objectArrayWithKeyValuesArray:responseObject];
        
        [self.tableView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self.tableView.mj_header endRefreshing];
        
        [SVProgressHUD showWithStatus:error.localizedDescription];
    }];
}

//上拉加载更多
-(void)loadMoreData{
    
}

#pragma mark - UITableView datasource & delegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.items.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    SubTagCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    
    cell.model = self.items[indexPath.row];
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
